package com.yesep.learn.design.behavioral.strategy;

import com.yesep.learn.design.common.pojo.*;

import java.util.*;

public class ShoppingContext {
    private IStrategy strategy;


    private List<Goods> goodsList;

    public ShoppingContext(List<Goods> goodsList) {
        this.goodsList = goodsList;
    }

    public static DoubleSummaryStatistics calculateTotalPrice(List<Goods> goodsList, IStrategy... strategys) {
        ShoppingContext shoppingContext = new ShoppingContext(goodsList);
        List<Double> prices = new ArrayList<>();
        for (IStrategy strategy : strategys) {
            shoppingContext.setStrategy(strategy);
            double price = shoppingContext.calculateTotalPrice();
            prices.add(price);
        }
        return prices.stream().mapToDouble(e -> e).summaryStatistics();
    }

    public void setStrategy(IStrategy strategy) {
        this.strategy = strategy;
    }

    public List<Goods> getGoodsList() {
        return goodsList;
    }

    public void setGoodsList(List<Goods> goodsList) {
        this.goodsList = goodsList;
    }

    public double calculateTotalPrice() {
        return strategy.calculateTotalPrice(goodsList);
    }
}
